cleanup, matrix calculation improvements