Refactor grid shape measurement