use error builtin function
refactoring while-loop