Implemented clean join of threads