refactor exception handling