improve the efficiency and generalization
nearly finish hw5