Rewritten pseudo-code in section 4.2.