refactor to use when.try