Fix bug in looping