Handling thread close