Refactoring and some fixes backported from 05-Structured.