some code refactoring and clean up