Added debug option, code cleanup, code comments