refactor gradient utils