"connection is invalid" error

Every time I try quitting an application in my script (most notably Terminal and sometimes TextEdit), the error “connection is invalid” pops up and the script halts.

I have no clue as to what in my script could be causing this. Here is the portion in question, excised from the larger script as a completely standalone script that still produces the error:

tell application “Terminal”
activate
do script “./sybase.sh” in window 1 --all this does is copy files from one location to another
end tell

set wait to 0
repeat until wait = 20 --to wait 20 seconds until script is done running
delay 1
set wait to wait + 1
end repeat

tell application “Terminal”
do script “exit” in window 1-- to gracefully exit from Terminal
close window 1
quit
end tell

I originally tried, in place of the last tell block: "quit application “Terminal” ", but this created the “connection is invalid” error also.

Does anyone know what causes this?