putting test in function to simplfy recursion