Fix a subtle error caused by reusing the variable index.