fix issue #3 UnboundLocalError and #4 UnicodeEncodeError