revised sgd functions