Updated the algorithm slightly.