Algorithms refactoring (no change in behavior).