Improve code to display error messages