Moving the function from the bottom of the script to just after all files are transferred to the installation folder appears to have fixed the problem.