WORKING encode and decode