Maybe I misread the problem, but that looks more complicated than I imagined.
Glad it is working for you.