Various code cleanup, mostly Python exceptions

should not affect behavior