Code extra defensively to prevent a weird sporadic bug I recently encountered.