Fixed race condition in a better way