Refactoring the code using methods