Fix entity insertion with 2 entities on same line

This is for #6249 .

I added and renamed vars, so diff is not quite as complicated as it looks.