I found a video on YouTube with a sercol and a beam, and everything seems to be fine, but the beam doubles and flickers after spawning from the mirror, tell me what could be the problem, screenshot and the code is attached.

extends Node2D
onready var ray = $RayCast2D
onready var lain = $Node2D/Line2D
var vec = Vector2()
var max_bounces = 5
func _process(delta):
lain.clear_points()
vec.y-=1
lain.add_point(Vector2.ZERO)
ray.global_position = lain.global_position
ray.cast_to = vec.normalized()*1000
ray.force_update_transform()
var prev = null
var bounces = 0
while true:
if not ray.is_colliding():
var pt = ray.global_position + ray.cast_to
lain.add_point(lain.to_local(pt))
break
var coll = ray.get_collider()
var pt = ray.get_collision_point()
lain.add_point(lain.to_local(pt))
if not coll.is_in_group("Mirrors"):
break
var normal = ray.get_collision_normal()
if normal == Vector2.ZERO:
break
if prev !=null:
prev.collision_mask = 3
prev.collision_layer = 3
prev = coll
prev.collision_mask = 0
prev.collision_layer = 0
ray.global_position = pt
ray.cast_to = ray.cast_to.bounce(normal)
ray.force_raycast_update()
bounces+=1
if bounces >= max_bounces:
break
if prev !=null:
prev.collision_mask = 3
prev.collision_layer = 3