Replace while-loop by for-loop

Just to clarify the many pull requests: I'm trying to remove all generic (non-GPU) related changes out of my private branch and into the open.