Made significant changes to algorithm.