Warning fixes and cleanup

I hope this is ok. The were several cases of mixing tabs and spaces so easiest thing for me was to just let autopep8 reindent everything instead of going over manually.

Please see individual commit with ?w=1 for changes.
Also fixes some unrecoverable errors.