Improved thread safety, unicode support, some code cleanup