Started restructuring code moved functionality out of main()