added support for arbitrarily large grids