added another way to calculate gradient