Fix some deprecated exception styles and performance improvements

These are some super simple improvements without changing the API.

This is also a first step to making python 3 compatible. I'll submit a python 3 patch next.