Formatted some of the code and added commenting.