PEP8 compliance, linting, redefined shadowed variables, removed global variables

…and miscellaneous other fixes