Refactoring: reduced duplicated code. Left code duplication in Main for readability