corrected function counting distance between objects