Refactored code and modularized the code