Updated simulator to handle loops