improved the efficiency of the algorithm