Moved a few blocks of code in main function.