Fix bugs with factory