Threads exiting loop properly