Added in logging
Refactored a bit