cleanup and minor refactoring