Partially based on http://bost.ocks.org/mike/bar/2/