Refactoring of init function.  Also fixed flicker when loading first document