Improved debug function