refactored code to reduce