refactor out common  function