Fixing bug found by pycharm code inspection
Fixing a bunch of small bugs raised by PyCharm, mostly cosmetic