Update main function and fix some bugs.