Function to extract innermost nested element