Fixed bug in Document and made some other improvements