better error handling and diagnostics