Rewrote the core layout stuff. Sortof making sense.

It's still very much a WIP - I'm beginning to see where all the
corner cases come from.