Better gradient function.