Refactor pages controller