Added encode function.