broke gradient calcuation into i, j, k functions to be more efficient