Fix thread bugs.