A man who kidnapped a 14-year-old girl when she accepted his offer of a ride home from school because her feet were sore, held her captive for nine months and raped her repeatedly at his trailer acknowledged his crimes on Thursday and apologized. The girl, who was in court to hear his admission, thanked him for eventually letting her go.

Nathaniel Kibby pleaded guilty to seven counts including kidnapping, aggravated felonious sex assault and criminal threatening. He was sentenced to 45 to 90 years in prison.

Kibby, who had pleaded not guilty after his arrest, had been scheduled to go on trial next month on nearly 200 felony charges related to the girl's October 2013 disappearance and the months that followed. But he changed his pleas to guilty at a hearing on Thursday.

Before the 35-year-old Kibby entered his new pleas, a prosecutor said Kibby had kidnapped the girl by offering her a ride home from her school and then brandishing a gun when she tried to get out of his car.

Prosecutor Jane Young said the girl and Kibby didn't know each other and she accepted the ride because she'd worn boots to school that day and her feet were blistered. Young said when the girl tried to get out of the car in a parking lot Kibby pulled out the gun and threatened to "blow her brains out" and slit her throat.

Last week, a judge ruled Kibby's lawyers could not question the girl before his trial about her exposure to media coverage of the case and the amount of freedom she was given to move about his trailer in Gorham, where prosecutors say he used a stun gun, zip ties and a shock collar to control her.

Kibby was charged with kidnapping the girl on Oct. 9, 2013, as she walked home from her high school in Conway. The girl returned to her home in North Conway the night of July 20, 2014.

Young said Kibby released the girl because he was convinced he'd "terrorized her enough" that she wouldn't reveal his identity.

The girl waited until a week after she was home to reveal Kibby's name, which she had seen inside a cookbook in his home.

The girl, who is now 17 years old, told a packed courtroom she thinks daily about the crimes Kibby committed against her. But she also thanked him for freeing her in July 2014.

"I want you to know that I appreciate my freedom because of you and I enjoy my life because of you," she said. "I just want to thank you for giving me my freedom back."

Kibby apologized for the decisions he made and said he didn't want to discuss his feelings in front of the journalists in the courtroom.

Young said that early in the girl's captivity Kibby instructed her to write a misleading letter home in an effort to throw off authorities, who'd launched a massive search for her. The girl still had fake nails at the time and carved his identity and vehicle information into the letter, but he caught on, zapped her with a stun gun as punishment and made her write a new letter.

Lawyers hired by the girl's family said she had suffered "numerous acts of unspeakable violence" during her months of captivity. Their statement was largely a plea for privacy and did not elaborate on what she endured.