Add attach/detach methods; refactor unit tests