Fixed loop code problem