fix bug and optimize the program