Refactored to use Input and Output streams