First version with custom distance function