modify finish_encode line87