refactored d3 code