Refactoring: extract code from main to a method