refactored Main()