Added doc and tests for encode