Fixed up logic for loop variables context