further small optimizations
switch to sparse matrix and protect against very large matrices