First version of encode