Restructure tasks to avoid deadlock