def METHOD_NAME():
    """
    Handle the FORWARD state.
    In this state we know there is a wall to our left, so we move forward
    and correct our path to follow the wall.
    """
    updateDistances()
    # We ran into a wall, we need to go backward.
    if (distance[2] <= HIT_WALL_DISTANCE or
       distance[3] <= HIT_WALL_DISTANCE or
       distance[4] <= HIT_WALL_DISTANCE or
       distance[5] <= HIT_WALL_DISTANCE):
        setMotors(0, 0)
        return ST_BACKWARD
    # Side sensor can't see the wall, we reached an edge.
    elif isinf(distance[0]) or distance[0] > (2 * distance[15]):
        # Are the front sensors seeing anything? If yes we need to turn right.
        if (distance[1] < MAX_WALL_THRESHOLD or
           distance[2] < MAX_WALL_THRESHOLD or
           distance[3] < MAX_WALL_THRESHOLD):
            setMotors(MAX_SPEED, MAX_SPEED / 2)
        # Otherwise we need to turn left.
        else:
            setMotors(MAX_SPEED * 0.5, MAX_SPEED)
    # There is a wall in front of us, we need to turn right.
    elif (distance[1] < MIN_WALL_THRESHOLD or
          distance[2] < MIN_WALL_THRESHOLD or
          distance[3] < MIN_WALL_THRESHOLD or
          distance[4] < MIN_WALL_THRESHOLD or
          distance[5] < MIN_WALL_THRESHOLD or
          distance[6] < MIN_WALL_THRESHOLD):
        setMotors(MAX_SPEED * 0.5, MAX_SPEED * 0.5)
        return ST_RIGHT
    # Too close to the wall, we need to turn right.
    elif distance[0] < MIN_WALL_THRESHOLD or distance[15] < MIN_WALL_THRESHOLD:
        # Special case to avoid division by 0 when we are very close.
        if distance[15] == 0:
            if distance[0] == 0:
                setMotors(MAX_SPEED, MAX_SPEED * 0.5)
            else:
                setMotors(MAX_SPEED, MAX_SPEED)
        # We are already heading away from the wall, no need to turn any further.
        elif distance[0] > distance[15]:
            setMotors(MAX_SPEED, MAX_SPEED)
        else:
            ratio = distance[0] / distance[15]
            setMotors(MAX_SPEED, MAX_SPEED * ratio)
    # Too far from the wall, we need to turn left.
    elif distance[0] > MIN_WALL_THRESHOLD:
        # We are already heading toward the wall, no need to turn any further.
        if distance[0] < distance[15]:
            setMotors(MAX_SPEED, MAX_SPEED)
        else:
            ratio = distance[15] / distance[0]
            setMotors(MAX_SPEED * ratio, MAX_SPEED)
    # We are in the right direction, or almost in the right direction.
    else:
        ratio = distance[15] / distance[0]
        absRatio = max(ratio, 1 / ratio)
        # A correction is needed.
        if absRatio > 1.01:
            # We need to correct to the right.
            if distance[15] > distance[0]:
                setMotors(MAX_SPEED, MAX_SPEED / absRatio)
            # We need to correct to the left.
            else:
                setMotors(MAX_SPEED / absRatio, MAX_SPEED)
        else:
            setMotors(MAX_SPEED, MAX_SPEED)