improved grid function to add nodes