refactored code to be more modular