added extensive comments and very minor cosmetic code chenges