further refactoring
moved error messages to a separate function