probably easier to first get `children` and then call `children.push(element)`. Would save you this loop.

Moreover the top-level element would be at the end, as it was before your changes.