general cleanup and minor refactoring