changed mergeMatrices to account for caller environment