Fix thread safety issues.