Agent threads are refactored. Ready for tests