This is most of the fix for bug #6937.  I just need to remove the old stuff.