Fix some potential race conditions.
Merge pull request #2 from jkolb/bugfix1

Fix some potential race conditions.