added check for unicode before encoding