refactored out some code duplication