Various race condition fixes.