fix algorithm