A bunch of refactoring