Amazing that you spotted this. You're absolutely right. I merged the two variables into one called lastMatchIndex. I had to tweak the order of a couple of lines and add a simple check for that initial case, but otherwise it worked just fine.