Combined code for encode and decode. Started on extract for specific index.