Tidy up of code and some logging.