Refactored structure-generation code.