Use fast version for D

Apply fast version and move `dist` function to where it is used.