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