Cleaned up some of the code and added some commets.