Clean up and add error check in SumOfMatrices