working version for bs distance