Improving the pageState detection code