refactored to include cases in recursion