Fix thread safety