Js code refactoring