Fixed the attach and detach tasks to be more robust