Restructered Main Code