Expect Script TCL ending early

I have the following expect script which works for the most part. I have it connecting to a Cisco switch (eventually a couple hundred) and configuring syslog servers. It checks to make sure that buffering wasn’t already configured and if it isn’t, set it.

If “logging buffered” is set, the script works fine. However, if “no logging buffered” is present, the script dies early before saving the config.

    set timeout -1
    set ip [lindex $argv 0]
    set host [lindex $argv 1]
    set username "admin"
    set password "password"
    set prompt "#|%|>|$ $"
    set timestamp [timestamp -format %Y-%m-%d_%H:%M]
    
    log_file ./$host.$timestamp.log
    
    spawn ssh -o StrictHostKeyChecking=no $username@$ip
    
    match_max 100000
    
    expect {
        "assword:" { send "$passwordr" }
        "continue connecting (yes/no)?" {
        send "yesr"
            expect {
            "assword:" { send "$passwordr" }
            }
        }
    }
    expect "#"
    send -- "r"
    expect "#"
    send -- "logging onr"
    expect "#"
    send -- "conf tr"
    expect "#"
    send -- "do show run | inc logging bufferedr"
    expect {
            "no logging buffered" {send "logging buffered 16384r"; exp_continue}
            "#"  {send "r"}
           }
    expect "#"
    send -- "logging host 1.2.3.4 transport tcp port 514r"
    expect "#"
    send -- "logging host 1.2.3.5 transport tcp port 514r"
    expect "#"
    send -- "logging trap notificationsr"
    expect "#"
    send -- "endr"
    expect "#"
    send -- "wr memr"
    expect "#"
    send -- "exitr"

With “no logging buffered” present

spawn ssh -o StrictHostKeyChecking=no admin@192.168.1.2
(admin@192.168.1.2) Password:

core-switch#
core-switch#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
core-switch(config)#logging on
core-switch(config)#do show run | inc logging buffered
no logging buffered
core-switch(config)#logging buffered 16384
core-switch(config)#
core-switch(config)#logging host 1.2.3.4 transport tcp port 514
core-switch(config)#logging host 1.2.3.5 transport tcp port 514
core-switch(config)#logging trap notifications
core-switch(config)#end
root@ubuntu:~/test#

With “logging buffered xyz” present

root@ubuntu:~/test# ./config-syslog-password.sh
spawn ssh -o StrictHostKeyChecking=no admin@192.168.1.2
(admin@192.168.1.2) Password:

core-switch#
core-switch#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
core-switch(config)#logging on
core-switch(config)#do show run | inc logging buffered
logging buffered 16384
core-switch(config)#
core-switch(config)#logging host 1.2.3.4 transport tcp port 514
core-switch(config)#logging host 1.2.3.5 transport tcp port 514
core-switch(config)#logging trap notifications
core-switch(config)#end
core-switch#wr mem
Building configuration...
[OK]
core-switch#root@ubuntu:~/test#

Now I know its something to do with the TCL but I can’t figure out what. Any help will be appreciated.