Refactored code to make more modular.