refactor code for exeption