refactoring d3 code