Added gradient computation code.