implemented new algorithm 2