code refactoring and cleanup