added an alternative gradient implementation