rewrite using pure javascript