Refactoring the code to make it more modular