Revised the main function