refactoring and code cleanup