Implemented clean thread handling