small optimization, avoid insert and pop

Instead replaces last value of list. From what I can see, this is the intention of the previous code.