Fixes for Python 3 support and some potential performance improvements for Python 2