Bug fix and improved error reporting and handling