algorithm 1,2 corrected