Fixed multiple issues for factory generation. Code exists as a duplicate though, so needs refactoring.