this line wasn't getting called for the last doc of each chunk, so you would get 1/chunk_size docs missing from your index. the rest is just removing dead comments and cleaning up the loop to make an error like this more obvious