refactored some code to stay bone DRY