Refactoring and adding logging with comments