ALGORITHM 1 AND 2