Logic for inserting documents