Not *quite* there but the concept is in place.  May need some debugging.