some refactoring and clean up