improve the efficiency and correct a bug