refactoring and cleaning up the code