Fix some potential race conditions.