Rearrange statements to solve deadlock