Improve unserialize logic