Restructured program and added comments