Refactor gradient calculations.