a bit of refactoring and cleanup