refactor error handling, add error messages