Trap the temp file in chgpw()
Trap EXIT also
All caps for global constants
remove useless tests
In trap, expand $swap at trap declaration

We know that the value of $swap will not change. And, if we wait to
expand it, we get an error about it being unset... don't really
understand what is happening there.