Adding log messages and refactoring a bit