Fixed, commented, and improved code