main algorithm...