Various small fixes

I was bored and ran `make check` through Address Sanitizer. It didn't actually find anything outside the test code, but here's a fix. clang also spit out a ton of warnings. I skimmed through it and addressed some of the more obvious ones. I think there's one legitimate bug (e1cc7a98fc757c56f896445ca57ccc573410afd1) in here, but the rest isn't very interesting.