Fix some invalid casts if certain element nesting occurs