Fix thread save problems.