Optimize the code using np.rollaxis (proposed by josef-pkt)