Refactored while loops