Main thread while loop fix