Changed the algorithm a little.