Restructure to use onDocument load correctly