Refactor the reachable function