Use utf-8 encoding to write to file. Fix some bug.