Finished the grid function