Refactor

Ended up refactoring to tidy it up a bit and possibly make extending it
easier.