Stupidly large PR - tests, cleanup, refactoring

Anyway, erm, look through. Test it if you can.

I have started to add tests for parts; if you could maybe give me the data that you have been using to test it, I can formalise those tests. Also, I'm going to try to add some in-code developer documentation, just so *I* can understand what is going on :P

Erm, yeah, yet me know if i've done something stupid :P