Rewriting this in Python...