Added documentation, and various aesthetic changes to the code