rewrite from pyeco