Updated encode and decode functions