Better support for dealing with large galleries

This isn't exactly exhaustive, but generally speaking one will run into a lot of problems when trying to deal with large matrices/galleries, both in terms of memory use, and things just outright crashing. These commits address a few of the relevant issues