Fix possible issue with foreach and local variable