refactor main function add some comments