changed to pointer to document, since we need to preserve state of
document
Mark errors and disconnect on the document