Rearranged some torch code