Adds some more error handling to parse_document