Fixed a cache problem by duplicating the layout stack when caching it. Otherwise the reference is shared between caches and we can end up rendering too many layouts.