refactor js code