Optimize and simplify gradient