first version of gradient function