Adapted for Python 3