added code for nearest distance