first refactor error handling