semi-related note:
The checks in these two if statements
if index % 500:
foo
and
if index % 500 == 0:
foo
are the logical negations of each other, at least in languages where any positive non-zero number evaluates to true.
So remembering that '==0' is important.