Refactored logging
Fixed some bugs