Fix a bug in algorithm.