refactor algo