improve main function