Refactoring and cleaning up code