a bit of refactoring and style clean up