Eliminate unnecessary arrays and call to flatten