Refactor debug functionality