attached/detached hook logic