changed logic for the loop