Upstream fixes

I've compiled it with clang. It found two bugs and issued one warning. Here they are.