Task 5_6 tweaked to avoid deadlock