major refactoring to use factory