Fixed some faulty logic in thread_run()