Refactor Javascript