refactoring and added verbose logging