fix to matrix calculation