A bit of refactoring and cleanup