Refactoring and cleaning the code