Fix gradient function.