Maybe it's easier to use the original code and call _importReferences() after each 1000 entries. This avoids a lot of things you have currently to think about.