Modifed view_doc to first check if the document/session is valid