Algorithm refactoring