Fix unsafe loop variables (thanks to backflash)
Avoid global variable leaks
Merge remote-tracking branch 'upstream/3.0' into 3.0