Fix thread handling