faster recursive method