Refactored loop to clarify logic