change to sparse matrix implementation