refactored main class and encapsulated output writing