Made the iteration to find the direction