Fix race conditions demonstrated via test race.

Fixes issue #84