Refactored to make grid page selection more robust