refactored code to be less repetitive