Code cleanup and commenting for the sake of documentation