Fix for the grid generation in the presence of particularly strange grid types. [originally from svn r8750]