some more refactoring and cleanup