finished the algo code