Fixed a race condition causing infinite loops