added Encode and Decode functions