Added debug mode, minor code cleanup