Add logging.
Refactor.