refactored away some code duplication