I've already left it as a comment (actually, three comments!) on the OP's MathOverflow (self-)answer. I'd like to be a little more sure that I've understood everything perfectly (e.g. implement the algorithm myself), and put it in a more coherent form, before posting as an answer on MathOverflow. :-)