Eliminated race condition in thread