Introduced logging, and refactored code