Oops; I'll tidy this up before it's merged! But please do look through the rest of the code!