Fixed factories, unit tests and ApiLibrary cleanup