Refactor

Here are some refactorings. They depend on #1309, so need to wait for r+ on that first.

r? @metajack