Added thread close in code