Fixed the out of bounds error in the recursive functions.