Refactoring javascript tree