refactoring and cleaning up code