$target?

I'm not sure what it is or if it works. I would debug a little. In the line before you use $target, try "echo -s target is: $target " to see if $target does link to anything.

Either way, I would use $chan instead.