Refactor this as stated above using spinning critical section rather than mutex.