Changed some things to the algorithm.