Refactored the error messages into its own function