Fixed bug in thread closing