Refactor start and close modules in agent.js