Cleaned out a lot of cruft, refactored code, tidied up generally.