Refactored a while() into a for()