Another batch of trivial pylint warning fixes

These changes fix some more cosmetic pylint warnings, and put some code and variables which were in the globals scope into functions, silencing warnings and reducing the risk of accidental aliasing.