First attempt at some refactorisation.

Refactorisation here we come! Untested. Refer to the live-v1 tag for live
changes.