Refactoring and cleaning up