some code clean up and minor refactoring