Fix wait/detach logic