Refactored factory and made deserialization error more explicit