factored out file inserting