A bunch of warning fixes and small portability fixes

Also ran the code against cppcheck, which didn't find anything interesting.