avoid spurious permuations in gridify (doesn't seem to make much of a difference, so use the code ordering)