solved #31, created a better recursive alg