Refactoring and adding Logging