Updates from pycharm static code analysis. Fixes various obscure problems.