fixed error in new algorithm 2