refactor code -- move shared functions to d3methods