Various bug fixes and better logging.