Added debug logging.
Make it more modular.