faster gradient implementation