def get_gs_distortion(dict_energies: dict):
    """Calculates energy difference between Unperturbed structure and most favourable distortion.
    Returns energy drop of the ground-state relative to Unperturbed (in eV) and the BDM distortion that lead to ground-state.
    Args:
        dict_energies (dict): 
            Dictionary matching distortion to final energy, as produced by organize_data()
    Returns:
        (energy_difference, BDM_ground_state_distortion)
        """
    if len(dict_energies['distortions']) == 1:
        energy_diff = dict_energies['distortions']['rattled'] - dict_energies['Unperturbed']
        if energy_diff < 0 :
            gs_distortion = 'rattled' #just rattle (no BDM)
        else:
            gs_distortion = "Unperturbed"
    else:
        lowest_E_RBDM = min(dict_energies['distortions'].values()) #lowest E obtained with RBDM
        energy_diff = lowest_E_RBDM - dict_energies['Unperturbed']
        if lowest_E_RBDM < dict_energies['Unperturbed'] : #if energy lower that with Unperturbed
            gs_distortion = list(dict_energies['distortions'].keys())[list(dict_energies['distortions'].values()).index( lowest_E_RBDM )] #BDM distortion that lead to ground-state
        else:
            gs_distortion = "Unperturbed"
       
    return energy_diff, gs_distortion