Rewrite in pure javascript