fix wrong algorithm