Return error on sparse matrices instead of crashing.
Some more error checking.