a bit more refactoring and cleanup