added encoding function