Rewrote flatten