Now with output logging.
Refactored.