Fix detach code