Added encoding function