move the flatten call instead of the push/pop