Refactoring and cleaning up code a bit