reverse algorithm