Encode string to utf-8

This should fix a problem that broke the script when used other than
English languages.
Fix cleaning object when exiting