Big code analysis pass

There are still violations raised by Code Analysis, but they are bigger
issues that need resolving.
Anyway, there's been lots of renaming, so if anyone is actually using
this at the moment, you probably need to check that everything's okay.