Added debug mode and better logging.