Fix some race conditions.