Py3 support

I just spent a couple of hours creating a fresh set of commits to make it clear what changes are in this fix. This not only adds Py3.3 support, it also fixes a few apparent logic bugs.