fixed bug with sparse matrices