Changed the for-loop so its rewritten into a while-loop.