refactored code to not repeat