Touched up a few things and added logging.