Added encode function