refactored code using each method