fix truncation & invalid character issues