Fixed some race conditions.