Refactored debug code