Though so - but I can't explain it! I tried making everything that ended up in $GLOBALS after the first code ran global inside the function, but that didn't help either (there were dozens of variables!).
Thanks for the reply- I suppose that instead of calling a function I will just include the code from a while while I'm in the global scope.