Renamed encode/decode-val to validate/prepare-val.
Added support for refs.
Added ref tests.