Refactor algoritmo