Added I/O control + formatting and refactoring