upgraded the main function