Yeah, I'm just dropping the prose-y version again.  It's too hard to write a non-trivial algo in prose without it getting stupidly complex.