main function improved