added gradient computation