Huge rearchitecture.

The whole thing seems a lot bigger and more complex now but it could
almost all be tested thanks to this change.
Should be easier to implement #5 and #8 as well.