Refactor js code