Fix a bunch of mostly not-really-problems, and one or two real ones.
Detected by cppcheck.