refactor incrementally to JS