replaced while-loop with for-loop