support for utf-8 and added missing variable 'written' declaration