tidy up grid code a bit