Refactor the de/serialization of a model

omfg this took forever.