Refactor, and improve error messages (cf Trac #3395)