Rather than modifying global variables. Make local copies of these when you need to modify them. I don't think they are used outside of this function, so that should work.