Refactor threads communication logic.