Fix bugs in algorithm 2. Stress test.