Refactor debug functions