Bug fix in grid calculation