fixed more on the algorithm
better algorithm