Rewritten in Python