Fixed the thread waiting problem