Fix undeclared globals in functions and shorten lines in misc_helpers.lua.