Changed the grid algorithm