         super.viewWillTransition(to: size, with: coordinator)
         
         coordinator.animate(alongsideTransition: { (context) in
-            if let popover = self.selectedArticlePopover,
-                let annotation = self.mapView.selectedAnnotations.first,
-                let annotationView = self.mapView.view(for: annotation) {
-                self.adjustLayout(ofPopover: popover, withSize: popover.preferredContentSize, viewSize: size, forAnnotationView: annotationView)
-            }
-            
             if let groupVC = self.placeGroupVC, let annotationView = self.placeGroupAnnotationView  {
                 let center = self.view.convert(annotationView.center, from: annotationView.superview)
                 groupVC.layout(center: center)
+            }
+            
+            if let popover = self.selectedArticlePopover,
+                let annotationView = self.selectedArticleAnnotationView {
+                self.adjustLayout(ofPopover: popover, withSize: popover.preferredContentSize, viewSize: size, forAnnotationView: annotationView)
             }
         }, completion: nil)
     }

        var x = annotationCenter.x > viewCenter.x ? viewSize.width - popoverSize.width - spacing : spacing
        var y = annotationCenter.y > viewCenter.y ? viewSize.height - popoverSize.height - spacing : spacing

        let fitsTopOrBottom = (top < 0 || bottom < 0) && viewSize.width - annotationCenter.x > 0.5*popoverSize.width && annotationCenter.x > 0.5*popoverSize.width
        
        let fitsLeftOrRight = (left < 0 || right < 0) && viewSize.height - annotationCenter.y > 0.5*popoverSize.height && annotationCenter.y > 0.5*popoverSize.width
        
        if (fitsTopOrBottom) {
            x = annotationCenter.x - 0.5 * popoverSize.width
            y = annotationCenter.y + (top < bottom ? 0 - totalHeight : popoverDistanceFromAnnotationCenterY)
        } else if (fitsLeftOrRight) {
            x = annotationCenter.x + (left < right ? 0 - totalWidth : popoverDistanceFromAnnotationCenterX)
            y = annotationCenter.y - 0.5 * popoverSize.height
        } else if (top < 0) {
            y = annotationCenter.y - totalHeight
        } else if (bottom < 0) {
            y = annotationCenter.y + popoverDistanceFromAnnotationCenterY
        } else if (left < 0) {
            x = annotationCenter.x - totalWidth
        } else if (right < 0) {
            x = annotationCenter.x + popoverDistanceFromAnnotationCenterX
        }
        
        articleVC.view.frame = CGRect(origin: CGPoint(x: x, y: y), size: popoverSize)
    }
    

        }
    }
    
    enum PopoverLocation {
        case top
        case bottom
        case left
        case right
    }
    
    func adjustLayout(ofPopover articleVC: ArticlePopoverViewController, withSize popoverSize: CGSize, viewSize: CGSize, forAnnotationView annotationView: MKAnnotationView) {
        var preferredLocations = [PopoverLocation]()
        
        if let groupAnnotationView = placeGroupAnnotationView {
            let adjustedCenter = view.convert(annotationView.center, from: annotationView.superview)
            let adjustedGroupCenter = view.convert(groupAnnotationView.center, from: groupAnnotationView.superview)
            let dx = adjustedGroupCenter.x - adjustedCenter.x
            let dy = adjustedGroupCenter.y - adjustedCenter.y
            
            if dy >= 0 && dx >= 0 {
                if dy > dx {
                    preferredLocations = [.top, .right]
                } else {
                    preferredLocations = [.right, .top]
                }
            } else if dy <= 0 && dx <= 0 {
                if dy < dx {
                    preferredLocations = [.bottom, .left]
                } else {
                    preferredLocations = [.left, .bottom]
                }
            } else if dy >= 0 && dx < 0 {
                if dy > abs(dx) {
                    preferredLocations = [.top, .left]
                } else {
                    preferredLocations = [.left, .top]
                }
            } else if dy < 0 && dx >= 0 {
                if abs(dy) > dx {
                    preferredLocations = [.bottom, .right]
                } else {
                    preferredLocations = [.right, .bottom]
                }
            }
        }
        
        let annotationSize = annotationView.frame.size
        let spacing: CGFloat = 5
        let annotationCenter = view.convert(annotationView.center, from: mapView)
        let viewCenter = CGPoint(x: view.bounds.midX, y: view.bounds.midY)
    
        let popoverDistanceFromAnnotationCenterY = 0.5 * annotationSize.height + spacing
        let totalHeight = popoverDistanceFromAnnotationCenterY + popoverSize.height + spacing
        let top = totalHeight - annotationCenter.y
