Refactoring for new algorythm