Added grid building function