Hi,
sorry I'm not 100% sure this is the right place to suggest that, but the following code can result a little unclear because you reference a variable index which is not previously defined:

Then, when double-checking whether the idea works, I think you mix up variable names a little:

Not a big deal but I think a 'fusion' of the two snippets into one would be clearer, as direct implementation of what you propose does not work.
Best,
M