reduce matrix/array copying

I’ve not tested these things, and did the changes without examining the
code in any depth, so it’s possible errors will crop up.