refactoed main()