pep8 fixex, better error handling, other minor refactors