Complete change of encode() API and implementation