rewrite with function