Improved the algorithm drastically.