refactored agent and device stuff