Cleaned up the page selection logic