Rewrite to python