Fix handling of empty sequences in flatten()