Fix out-of-bounds access to output array.
Better toMatrix performance.