Modifed code for Factory Pattern