Modified some of the code to include the Factory design pattern