Fix race condition, hopefully! Minor other changes.