Reorganized Functions and added comments in main()