Rewrite in python