Rearrange code to avoid gettting stucks sometimes.