extract helper functions and add error messages