                 self.imagePickerController.presentCamera()
             }
         } else {
-            selectAsset(atIndex: indexPath)
+            self.selectAsset(atIndex: indexPath)
         }
     }
     
     public func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
-        
-        if let removedAsset = (collectionView.cellForItem(at: indexPath) as? DKAssetGroupDetailBaseCell)?.asset {
-            let removedIndex = self.imagePickerController.selectedAssets.index(of: removedAsset)!
-            
-            /// Minimize the number of times.
-            let indexPathsForSelectedItems = collectionView.indexPathsForSelectedItems!
-            let indexPathsForVisibleItems = collectionView.indexPathsForVisibleItems
-            
-            let intersect = Set(indexPathsForVisibleItems).intersection(Set(indexPathsForSelectedItems))
-            
-            for selectedIndexPath in intersect {
-                if let selectedCell = (collectionView.cellForItem(at: selectedIndexPath) as? DKAssetGroupDetailBaseCell), let selectedCellAsset = selectedCell.asset, let selectedIndex = self.imagePickerController.selectedAssets.index(of: selectedCellAsset) {
-                    if selectedIndex > removedIndex {
-                        selectedCell.index = selectedCell.index - 1
-                    }
-                }
-            }
-            
-            self.imagePickerController.deselectImage(removedAsset)
-        }
+        self.deselectAsset(atIndex: indexPath)
     }
     
     public func scrollViewDidScroll(_ scrollView: UIScrollView) {

    
    // select an asset at a specific index
    public func selectAsset(atIndex indexPath: IndexPath) {
        guard let selectedAsset = (collectionView.cellForItem(at: indexPath) as? DKAssetGroupDetailBaseCell) else {
            return
        }
        
        selectAsset(cellToSelect: selectedAsset)
    }
    
    public func selectAsset(cellToSelect cell: DKAssetGroupDetailBaseCell) {
        guard let asset = cell.asset else {
            return
        }
        
        self.imagePickerController.selectImage(asset)
        cell.index = self.imagePickerController.selectedAssets.count - 1
    }
    
    func isCameraCell(indexPath: IndexPath) -> Bool {
        return indexPath.row == 0 && !self.hidesCamera
    }

}

// Show all images in the asset group
open class DKAssetGroupDetailVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, DKImageGroupDataManagerObserver, DKPhotoGalleryDelegate, UIGestureRecognizerDelegate {
    	
    public lazy var selectGroupButton: UIButton = {
        let button = UIButton()
