Change: Use nesting level for elements

Use a combination of the nesting level and the top offset to determine groups. Also go through the nesting level in reverse order to make sure that the most inner element gets calculated before the parent element to get the correct height.