working version with threads managed inside device