Refactoring and cleaning up of code