A bit more refactoring and cleanup