Fixed the reshaping bug at the end of the code.