Refactored into a sane base of classes

Hopefully well enough laid out to extend further in the future.