Miscellaneous refactorings.
Fixed the stuff that cppcheck found problems with.