refactored dg distance