modify algorithm 1