improve toplevel element handling in generated code