Fixed a few race conditions.