Fixed a crash relating to opening the same document twice.

More cleanup and working on stuff.