Index: branches/eam_branches/ipp-20191011/Nebulous/lib/Nebulous/Client.pm
===================================================================
--- branches/eam_branches/ipp-20191011/Nebulous/lib/Nebulous/Client.pm	(revision 40999)
+++ branches/eam_branches/ipp-20191011/Nebulous/lib/Nebulous/Client.pm	(revision 41005)
@@ -209,5 +209,5 @@
         },
     );
-
+    
     $log->debug( "entered - @_" );
 
@@ -310,4 +310,5 @@
     my $stats = $self->stat($key);
     my $instances = $stats->[6];
+
     if ((not defined $instances) or ($instances == 0)) {
         $self->set_err("can not cull - no instances");
@@ -315,5 +316,5 @@
         return;
     }
-    if ($instances < $min_copies) {
+    if ($instances <= $min_copies) {
         $self->set_err("can not cull - not enough instances");
         $log->debug("leaving");
@@ -403,13 +404,23 @@
     eval {
         # first - strip off any inaccesible instances
+	# these are not counted in the 'removed' number
         if (not defined $self->prune($key)) {
             # use the prune() method to also determine if $key is valid
             die "invalid key: $key";
         }
-
-        # check to see if there is an instance on $vol_name that should be the
-        # sole survivor
+	
+        # check to see if there is an instance on $vol_name that
+        # should be the sole survivor.  This is a bit dangerous : what
+        # if the only instance is on an invalid volume?  what if an
+        # invalid volume is specified?
+
         if (defined $vol_name) {
             $locations = $self->find_instances($key, $vol_name);
+            if (not defined $locations) {
+                die "no instances on requested node";
+            }
+            if (scalar @$locations == 0) {
+                die "no instances on requested node";
+            }
         }
 
@@ -418,21 +429,25 @@
 
         if (defined $locations) {
-	    # XXX this is an example of passing 'find_invalid' to find_instances
-            my $instances = $self->find_instances($key, undef, 'find them all');
-            if (not defined $instances) {
+	    # first delete the valid instances (except the one we own)
+            my $instances_valid = $self->find_instances($key, undef, 0); # find all valid instances
+            if (not defined $instances_valid) {
+		# there should have been at least one valid instance?
                 die "no instances";
             }
-            if (scalar @$instances == 1) {
-                # only one instance nothing to do
-                return 0;
-            }
-            if (scalar @$instances == 0) {
+            if (scalar @$instances_valid == 0) {
+		# there should have been at least one valid instance?
                 die "no instances";
             }
-            foreach my $victim (@$instances) {
-                next if $victim eq $locations->[0];
-                $self->delete_instance($key, $victim);
-                $removed++;
-            }
+
+	    # we have more than one instance, delete the others
+            if (scalar @$instances_valid > 1) {
+		foreach my $victim (@$instances_valid) {
+		    next if $victim eq $locations->[0];
+		    $self->delete_instance($key, $victim);
+		    $removed ++;
+		}
+	    }
+	    # the prune above should have deleted any invalid instances, so we only
+	    # need to worry about the valid instances;
         } else {
             # nuke whatever
@@ -918,5 +933,5 @@
     unless(defined($params[0])) {
 	$params[0] = hostname() . ".0";
-#	print STDERR "Setting host to $params[0]\n";
+	# print STDERR "Setting host to $params[0]\n";
     }
 
@@ -1187,5 +1202,4 @@
 
     $log->debug( "entered - @_" );
-
 
     my $locations;
@@ -1380,4 +1394,8 @@
         }
 
+        if ($response->faultstring =~ /is valid object key/) {
+	    $log->logdie("parameter #1 to delete_instance did not pass the 'is valid object key' callback");
+        }
+
         $log->logdie("unhandled fault - ", $self->err);
     }
@@ -1542,4 +1560,15 @@
 }
 
+sub set_log_level
+{
+    my $self = shift;
+
+    my $log_level = shift;
+
+    $log->level($log_level);
+
+    return $self;
+}
+
 
 1;
