fail rather than undefined
encode types in serialization
make the type of keys public
move serialization specific bits to their own module