refactor encoding/decoding into types