Tried to do a bit of refactoring