This is the code I ended up with that implements the approach I described in a recent answer to another question about the same problemrecent answer to another question about the same problem
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/