Improved error handling when parsing