Yup. Dinner was what it took to sort out this little problem, which uses a bit of non-standard recursion to get the results that I needed. Sleep now.