Fix some potential threading issues.