Implementation with distances