Major restructuring to get automatic decoding/encoding