You can replace $chan with #
It's easier to type which might come in handy when it's used 40 times in one script (like yours). Just a tip.