moved flatten out of function..