Fixed circular references.

Circular references still won't work as expected, but at least it won't
crash the parser.
Changes layout saving place.