bug fix and improved debug output