Updated main implementation to check state of document.