wrap up algorithm refactor