Moving script into a function