Refactored some code for modular design