rewritten  function