trying to fix it
Finally finished the algorithm