refactoring and added some more log messages