Bugfix - moved logical code out of debug block