I'm sure other people can point out better ways to do what you're trying to do, but I'll just point out that
loop. Once you get out,
but in the outer scope.
inside the loop is getting overwritten every iteration of the loop.
at the top of the script would help point some of this out, as well.