Refactor pages controller

Some small refactorings, moving some logic into private methods, and reduce complexity of method,  in preparation to move them into the model layer or better still a service model