changed for loop logic