Fixed up old function, and added some comments