Re-wrote and added some comments