After testing, the sample code must have been old. The first call to encode is not needed.