Cppcheck

A number of things found by running cppcheck on the code. There is one behavioral change, the rest is just small code cleanups.

It hasn't really been tested except compiling and running the unit tests. A quick review before merging wouldn't hurt.