def _flat(commands):
    """Flatten commands while preserving order"""
    commands = [cmd for cmd in commands if cmd is not None]
    flattened = []
    for command in commands:
        if isinstance(command, str):
            flattened.append(command)
        elif isinstance(command, list):
            flattened.extend(command)
        else:
            raise TypeError('Invalid command: %s' % str(command))
    return flattened