agent: Fix issues with re-registration. Fixes #216