fixed algjrithm bugs