in reply to weirdness with delete

I tried following your code and explanation and didn't manage anything but a headache. Post some dumps that illustrate your datastructures rather than explaining in words what your code does.

Show me your flowcharts and conceal your tables.
I shall continue to be mystified.
Show me your tables, and I won't usually need your flowcharts;
they'll be obvious.

- Frederick P. Brooks, Jr., The Mythical Man-Month

Makeshifts last the longest.