rewrite function