add extend_val argument to pass the threshold for gate extension. #36