refactored code to be more DRY