some restructuring of main function

some cleanup