PEP8 and some bug fixes

See commit messages for details