added detach logic