updated the thread function in tsmain.cpp