Partial refactor

There's still more to do but I don't want to bundle this with feature/bug changes.