rewrite js function