use for-loop instead of while-loop