Added external document handling code