refactored code using methods