Updated algorithm
Updated algorithm 2