added  code for distance detection