Refactor

We're nearly at the point where we were before the great refactoring. I think this branch is ready for production.