Rewrite rec algo