Added a lot of compatibility code for Python2 + 3