change to algorithm 2