fixed error with counting distance