finished algorithm 31 (we think)