Starting to refactor js code