Better debug logging and small code enhancements