Cleanup and fix a bunch of stuff

Including a fix for #40, but also a bunch of stuff that isn't #40 but that I thought could possibly have been related.