Changed gradient algorithm.