Fixes for UTF-8 handling and option to write to a file directly