Intiate function as soon as button is pressed, not loop it over and over.

I am working off a template. I understand globals can be avoided but I have to use the provided template.

Why? If the template is flawed, fix it!