Added checking if a thread is finished before joining it