Updated gradient descent loop