Generalize encoding a little to support lists also