Optimize flattening of dynamicstmts with a let