Cleaning up the code and applying refactoring.