Rework of #2267

Reset w index at end of inner loop.
Clean-up of surrounding code to fit coding guidelines.

I have setdata in the branch name, but it's actually in GetData.