I think I fixed the race conditions.

Program still always ends on an index-out-of-bounds exception however.