Refactored with a While loop and an each iterator