refactoring out some duplications and complexities