Another tip is to look at each controlling variable in turn, and check where it is being initialized, changed and used.

Do that with k, place and index, for example.

;-)