Add many comments. Make some minor code changes