Added encode/decode document functions