fix some encode error and add exceptional handling