modified algorithm implementation