Refactored code to add more modularization