Safe removal of targets

Also close the target before removing it.