Some fixes

Same patch that I submitted to Blaze earlier (cherry-picked from that repo, in fact) plus a fix for the tests: on Linux, you have to pass `-pthread` to both the compiler and linker to get POSIX threads, or they simply won't link.

Haven't tried this on any other platform.