Fixed bug that didn't allow the thread to exit