algorithm 2 nearly complete.