Improved sparse matrix constructor