Changed the program to use factory pattern, hopefully patrially correctly.