added a check to only compute the matrix if it doesn't already exist