refactored code to simplify