I agree gavinksong. Sorry for not committing before. Initially I found your solution to be merely an alternative to the original. But after consideration, I think yours is better.
I have switch to gavinksong's solution as it appears to be more robust. I am open to arguments for/against either TSLF or Gavin's or another better option if anyone has interest.