use tail recursion instead of fold, more test cases