Fixed potential crash during encoding