Wait for thread to finish