better way to flatten