Added python3 support.
Note, tried to backport to py2.6, but lack of set comprehension means more
  of a redesign than I'm willing to do at present.