reorganizing grid code