Lint fixes

I used the various Lint tools in PyCharm and checked the entire code base. I found a few definite bugs, most of which are at least a year old. I also found some code that appears to be obsolete. I committed the most serious stuff first and the least serious last. Pull changes from @tarekziade as well.