This code is based on code from Elliot Bentley’s Block 774b87bf0f7482599419d1e7da9ed918.
For more information, see his blog post A better way to structure D3 code.