Restructured main function