Improved handling when looping ends