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 global scope into functions, which silences warnings and reduces the risk of accidental aliasing.