patcher: moved global vars into function

For thread safety. Suggested by Pavel Volkovitskiy.