more robust serialization and better error messages