Optimized flatten routine. Merge bug identified.