fixed #123 bug with flatten. Added lots of tempVars. YEAH!