Fixing bugs and making code compatible with Python 3