Modularize and abstract d3 code