seprate and improve debug logic