bugfix: make sure next/last statements refer to the correct loop