added extensive code comments