refactor using d3 array functions