refactor some debug logic