Added a lot of documentation and formatted some routines for clarity