                     x: frame.minX + elements.before!.layoutMargins.left,
                     y: frame.minY + elements.before!.layoutMargins.top,
                     width: measures.before!.width,
-                    height: frame.height - elements.before!.verticalMargins))
+                    height: measures.before!.height))
             }
 
             if elements.after != nil {
                 elements.after!.layoutWith(frame: CGRect(
-                    x: frame.minX + frame.width - measures.after!.width - elements.after!.layoutMargins.right,
+                    x: frame.maxX - measures.after!.width - elements.after!.layoutMargins.right,
                     y: frame.minY + elements.after!.layoutMargins.top,
                     width: measures.after!.width,
-                    height: frame.height - elements.after!.verticalMargins))
+                    height: measures.after!.height))
             }
 
-            let relevantBeforeWidth = elements.before?.widthWithMargins.add(spacing) ?? 0
-            let relevantAfterWidth = elements.after?.widthWithMargins.add(spacing) ?? 0
             elements.content.layoutWith(frame: CGRect(
                 x: frame.minX
                     + relevantBeforeWidth
                     + elements.content.layoutMargins.left,
                 y: frame.minY + elements.content.layoutMargins.top,
-                width: frame.width
-                    - elements.content.horizontalMargins
-                    - relevantBeforeWidth
-                    - relevantAfterWidth,
-                height: frame.height
-                    - elements.content.verticalMargins))
+                width: measures.content!.width,
+                height: measures.content!.height))
         case .vertical:
             if elements.before != nil {
                 elements.before!.layoutWith(frame: CGRect(
                     x: frame.minX + elements.before!.layoutMargins.left,
                     y: frame.minY + elements.before!.layoutMargins.top,
-                    width: frame.width - elements.before!.horizontalMargins,
+                    width:  measures.before!.width,
                     height: measures.before!.height))
             }
 
             if elements.after != nil {
                 elements.after!.layoutWith(frame: CGRect(
                     x: frame.minX + elements.after!.layoutMargins.left,
-                    y: frame.minY + frame.height - measures.after!.height - elements.after!.layoutMargins.bottom,
-                    width: frame.width - elements.after!.horizontalMargins,
+                    y: frame.maxY - measures.after!.height - elements.after!.layoutMargins.bottom,
+                    width: measures.before!.width,
                     height: measures.after!.height))
             }
 
-            let relevantBeforeHeight = elements.before?.heightWithMargins.add(spacing) ?? 0
-            let relevantAfterHeight = elements.after?.heightWithMargins.add(spacing) ?? 0
             elements.content.layoutWith(frame: CGRect(
                 x: frame.minX + elements.content.layoutMargins.left,
                 y: frame.minY
                     + relevantBeforeHeight
                     + elements.content.layoutMargins.top,
-                width: frame.width
-                    - elements.content.horizontalMargins,
-                height: frame.height
-                    - elements.content.verticalMargins
-                    - relevantBeforeHeight
-                    - relevantAfterHeight))
+                width: measures.content!.width,
+                height: measures.content!.height))
         }
     }
 

        if measures.after == nil {
            measures.after = elements.after?.sizeThatFitsWithMargins(frame.size)
        }
        if measures.content == nil {
            measures.content = elements.content.sizeThatFitsWithMargins(frame.size)
        }

        switch orientation {

            let relevantAfterWidth = afterWithMargins?.width.add(spacing) ?? 0
            let availableWidth = max(size.width - relevantBeforeWidth - relevantAfterWidth, 0)
            measures.content = elements.content.sizeThatFitsWithMargins(CGSize(width: availableWidth, height: size.height))
            let contentWithMargins = measures.content!.addMargins(ofElement: elements.content)
            return CGSize(
                width: relevantBeforeWidth + relevantAfterWidth + contentWithMargins.width,
                height: max(beforeWithMargins?.height ?? 0, afterWithMargins?.height ?? 0, contentWithMargins.height))

        case .vertical:
            let relevantBeforeHeight = beforeWithMargins?.height.add(spacing) ?? 0
            let relevantAfterHeight = afterWithMargins?.height.add(spacing) ?? 0
            let availableHeight = max(size.width - relevantBeforeHeight - relevantAfterHeight, 0)
            measures.content = elements.content.sizeThatFitsWithMargins(CGSize(width: size.width, height: availableHeight))
            let contentWithMargins = measures.content!.addMargins(ofElement: elements.content)
            return CGSize(
                width: max(beforeWithMargins?.width ?? 0, afterWithMargins?.width ?? 0, contentWithMargins.width),
                height: relevantBeforeHeight + relevantAfterHeight + contentWithMargins.height)
        }
    }
}
