Consider providing a benchmark for your changes, to ensure the new implementation isn't significantly slower.

Also, can use .flatten!, .compact! and sort! (all on separate lines, not chained) for better performance.