Now fully works without the endless loop