Merged the code for the grid.

2nd part of the manual merge.