Fix error with closing each loop