added pageState variable to track page info; modified code to run necessary layout code only on certain pages.