First working version of gradient function. By works I mean, it doesn't
crash.