Building margin grid function