Added functions forEachInGrid, mapGrid.
Changed loops with functions forEachInGrid, mapGrid, reduce, map, forEach.