sparse matrix with improvements recommended by brandon