move encoding/decoding to one place