Refactor: Localize scope of loop variables