Flatten logic added but it doesn't quite do what I need.