refactor error messages into their own function