Removing undefined variable  and simplifying the debug logic