fix gradient computation