Remove most (all?) compiler warnings and fix a few potential bugs in the
process as well.