A bunch of refactoring.