Fixed the non terminating thread