Fix exception handling so it'd work under Python3.
Port the code to classes.