Move last little bit of script into a function so local is available

When I run the script, I get this error:

    -bash: local: can only be used in a function

Not sure if it's correct, but this patch just moves that stuff into a function and runs it. Seems to work.