Vast refactoring.

Don't ask - just merge please :-).