some bug fixes and improved debug output