improved distance function and added a test case