Added debug mode, minor fixes