Refactored/improved debug logging