Updated to support non-ascii strings and respect their encoding