some clean up and minor refactoring