Rewrite nested function