complete javascript implementation