More robust algorithm.