Fixed out of range problem.
Fixed race condition.