Cleaned up logging and made it safer.