Changed method to a recursive function.