rewrite in pure js