fix distance matching bugs