I think I understand WHAT you need but not WHY.

It's a fair bit of code so ....