refactor document loading to be slightly more sane