How could index be -2? Im ok with the validation of line 419, but besides that I only see you removed underscore functions and used a for next block.  Didnt underscore worked for you?