detects the correct variables but does it multiple times for some reason