Most of this block of code was copied in from someplace else, not freshly written for this fix.