Added factory pattern and writing to file.txt