reorganized and ready for recursion