Cleaned up and added some comments