Some improvements in the algorithm.