TODO: change the loop in thread.c