simplify algorithm: using less variables.