Reimplementation in Python