solution to flatten