fix errors and add encoding support