fixed bugs in algorithm implementation