Refactors to eliminate use of flatten and compact