rewriting in javascript