Yes, I omitted a very important line of code, and pr2-eugin has corrected it, thank you.
Global variables are easy to use, but they are vulnerable, since between the time that you set a global variable, and the time that you use it, other processes have access to it. It's also difficult to document what a global variable is for, and when it contains a legit value.
A computing principle is to always minimize the scope, which is the visibility and accessibility, of your variables. Don't expose a variable any more than is necessary.