updated loop code