Fix some potential multi-threading issues.