Refactored code to be more modular